package com.yaj.controller;

import com.yaj.base.ResponseResult;
import com.yaj.constant.LeaveConstants;
import com.yaj.pojo.LeaveRequest;
import com.yaj.service.ILeaveRequestService;
import com.yaj.utils.UserThreadLocal;
import com.yaj.vo.LeaveRequestVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/leave")
public class LeaveRequestController {
    @Autowired
    private ILeaveRequestService leaveRequestService;

    /**
     * 申请请假
     *
     * @param leaveRequest
     * @return
     */
    @PostMapping("/apply")
    public ResponseResult applyLeave(@RequestBody LeaveRequest leaveRequest) {
        leaveRequest.setStatus(LeaveConstants.LEAVE_REQUEST_PENDING);
        leaveRequestService.save(leaveRequest);
        return ResponseResult.success();
    }

    /**
     * 获取所有请假申请
     *
     * @return
     */
    @GetMapping("/requests")
    public ResponseResult<List<LeaveRequestVo>> getLeaveRequests() {
        List<LeaveRequestVo> leaveRequests = leaveRequestService.getLeaveRequests();
        return ResponseResult.success(leaveRequests);
    }

    /**
     * 获取请假记录
     * @param id
     * @return
     */
    @GetMapping("/records/{id}")
    public ResponseResult<List<LeaveRequestVo>> getLeaveRecords(@PathVariable Long id) {
        List<LeaveRequestVo> leaveRequestVos = leaveRequestService.getLeaveRecords(id);
        return ResponseResult.success(leaveRequestVos);
    }

    /**
     * 审批请假
     * @param id
     * @return
     */
    @PutMapping("/approve/{id}")
    public ResponseResult approveLeave(@PathVariable Long id,  @RequestBody Map<String, Long> requestBody) {
        Long approverId = requestBody.get("approverId");
        leaveRequestService.approveLeave(approverId,id);
        return ResponseResult.success();
    }

    /**
     * 拒绝请假
     *
     * @param id
     * @return
     */
    @PutMapping("/reject/{id}")
    public ResponseResult rejectLeave(@PathVariable Long id,@RequestBody Map<String, Long> requestBody) {
        Long approverId = requestBody.get("approverId");
        leaveRequestService.rejectLeave(approverId,id);
        return ResponseResult.success();
    }
}
